package com.atu.websocket.websocket;

import com.atu.websocket.bean.Message;
import com.atu.websocket.utils.Convert;
import org.springframework.stereotype.Component;

import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @author: atu
 * @date: Create in 2021/7/13 10:06
 * @Description:
 */

@ServerEndpoint(value = "/websocket")
@Component
public class Websocket {
    private static Map<Long, Session> sessionMap = new HashMap<>();
    private static Map<Long, Long> bindMap = new HashMap<>();

    @OnOpen
    public void onOpen(Session session) {
        System.out.println(session.getId() + "  加入");

    }

    private void sendMessage(Session session, String text) {
        try {
            session.getBasicRemote().sendText(text);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @OnMessage
    public void onMessage(String params, Session session) {
        Message message = Convert.fromJson(params, Message.class);
        System.out.println(Convert.toJson(message));
        switch (message.getType()) {
            case 1:
                sessionMap.put(message.getFromId(), session);
                System.out.println(message.getFromId() + "  已注册");
                break;
            case 2:
                sendMessage(sessionMap.get(message.getToId()), Convert.toJson(message));
                System.out.println(message.getFromId() + "  已发送 desc");
                break;
            case 3:
                sendMessage(sessionMap.get(message.getToId()), Convert.toJson(message));
                System.out.println(message.getFromId() + "  已发送 candidate");
                break;
            case 4:
                bindMap.put(message.getFromId(), message.getToId());
                sendMessage(sessionMap.get(message.getToId()), Convert.toJson(message));
                System.out.println(message.getFromId() + "  已发送 createOffer");
                break;
            case 7:
                sendMessage(sessionMap.get(message.getToId()), Convert.toJson(message));
                System.out.println(message.getFromId() + "  已发送 candidate");
                break;
            case 8:
                sendMessage(sessionMap.get(message.getToId()), Convert.toJson(message));
                System.out.println(message.getFromId() + "  已发送 candidate");
                break;
        }
    }

    @OnClose
    public void onClose(Session session) {
        disconnect(session);
    }

    private void disconnect(Session session) {
        Set<Map.Entry<Long, Long>> bindEntries = bindMap.entrySet();
        Set<Map.Entry<Long, Session>> sessionEntries = sessionMap.entrySet();
        for (Map.Entry<Long, Session> sessionEntry : sessionEntries) {
            if (sessionEntry.getValue().getId().equals(session.getId())) {
                for (Map.Entry<Long, Long> bindEntry : bindEntries) {
                    if (sessionEntry.getKey().equals(bindEntry.getKey())) {
                        Message message = new Message();
                        message.setType(6);
                        sendMessage(sessionMap.get(bindEntry.getValue()), Convert.toJson(message));
                        bindMap.remove(bindEntry.getKey());
                        break;
                    }
                    if (sessionEntry.getKey().equals(bindEntry.getValue())) {
                        Message message = new Message();
                        message.setType(6);
                        sendMessage(sessionMap.get(bindEntry.getKey()), Convert.toJson(message));
                        bindMap.remove(bindEntry.getKey());
                        break;
                    }
                }
                sessionMap.remove(sessionEntry.getKey());
                break;
            }
        }
    }

    @OnError
    public void onError(Session session, Throwable error) {
        disconnect(session);

    }
}
